<template>
  <div class="send-msg-dialog">
    <el-dialog title="发送消息" v-model="isShowDialog" width="50%" :before-close="beforeClose">
      <el-form :model="sendMsgForm" :rules="sendMsgRules" ref="sendMsgFormRef" label-width="100px">
        <el-form-item label="消息内容" prop="msg">
          <el-input v-model="sendMsgForm.msg" type="textarea" :rows="4" placeholder="请输入消息内容"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="beforeClose('confirm')">发送</el-button>
        <el-button @click="beforeClose('cancel')">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
// Path: src/views/equip/sendMsg.vue
import { defineComponent, ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';
import { sendEquipMessage } from '/@/api/equip/index';
import lodash from 'lodash';
export default {
  name: 'sendMsg',
  setup() {
    const { proxy } = getCurrentInstance();
    const isShowDialog = ref(false);
    const sendMsgForm = reactive({
      msg: '',
      customer_id: '',
      equip_num: '',
    });
    const sendMsgRules = {
      msg: [{ required: true, message: '请输入消息内容', trigger: 'blur' }],
    };
    const beforeClose = (action, done) => {
      if (action === 'confirm') {
        proxy.$refs.sendMsgFormRef.validate((valid) => {
          if (valid) {
            sendEquipMessage(sendMsgForm).then((res) => {
              if (res.code === 0) {
                const success_flag = lodash.get(res, 'data.success_flag', false);
                if (!success_flag) {
                  ElMessage.error('发送失败');
                  return;
                }
                ElMessage.success('发送成功');
                isShowDialog.value = false;
              } else if(res.message){
                ElMessage.error(res.message);
                
              }
            });
          } else {
            isShowDialog.value = false;
            return false;
          }
        });
      } else {
        isShowDialog.value = false;
      }
    };
    const openDialog = (rowData={}) => {
      isShowDialog.value = true;
      sendMsgForm.customer_id = rowData.customerId;
      sendMsgForm.equip_num = rowData.equipNum;
    };
    return {
      isShowDialog,
      sendMsgForm,
      sendMsgRules,
      beforeClose,
      openDialog,
    };
  },
};
</script>
<style scoped lang="scss">
.send-msg-dialog {
  :deep(.el-dialog){
    height: 32vh;
  }
}
</style>
